package Test;

import java.util.Scanner;

/*
 *Description:
 *User:laoHu
 *Created with Intellij IDEA 2025
 *Data:2025.05.15
 *Time:09:32
 */
public class Test {

    //递归实现一个数每位之和
    public static  int theSum0fEachDigit(int n){
 if(n < 10)
     return n;
 return n % 10 + theSum0fEachDigit(n / 10);
    }

    public static void main8(String[] args) {
        System.out.println(theSum0fEachDigit(1));
        System.out.println(theSum0fEachDigit(11));
        System.out.println(theSum0fEachDigit(111));
        System.out.println(theSum0fEachDigit(1111));
    }

    //汉诺塔问题
    public  static  void move(char pos1,char pos2){
        System.out.println(pos1 + "->" + pos2);
    }
    public  static  void hanNoi(int n ,char star, char mid, char end){
        if(n == 1)
            move(star,end);
        else{
            hanNoi(n-1, star, end, mid );
            move(star, end);
            hanNoi(n-1, mid, star, end);
        }
    }

    public static void main7(String[] args) {
        System.out.println("第一次：");
           hanNoi(1,'A', 'B', 'C');
        System.out.println("第二次：");
           hanNoi(2,'A', 'B', 'C');
        System.out.println("第三次：");
           hanNoi(3,'A', 'B', 'C');
    }


    //递归实现斐波那契数列求第N项
    public static int fib2(int n){
        if(n == 1 ||  n == 2)
            return 1;
        return fib2(n-1) + fib2(n-2);
    }

    public static void main6(String[] args) {
        System.out.println(fib2(5));
    }

    //打印每一位
    public static void print1(int n){
        if(n < 10)
            System.out.print(n + " ");
        if(n >= 10){
        print1(n / 10);
        System.out.print(n % 10 +" ");
        }
    }

    public static void main5(String[] args) {
        print1(1234);
    }


    //递归求和
    public static  int sum(int n){
        if(n == 1)
            return 1;
        return n + sum(n -1 );
    }

    public static void main4(String[] args) {
        System.out.println(sum(10));
    }

    //递归N的阶乘
    public static int fac(int n){
        if(n == 1)
            return 1;
        return n * fac(n - 1);
    }

    public static void main3(String[] args) {
        System.out.println(fac(5));
    }

    //求最大值的重载
    public static int max(int x,int y){

        return (x > y) ? x  :y;
    }
    public static double max(double x, double y){
        return (x > y) ? x : y;
    }
public static int max(int x,int y,int z){
        int tem = 0;
      if(x > tem){
          tem = x;
      }
      if(y > tem ){
          tem = y;
      }
      if(z > tem)
          tem = z;
      return tem;
}

    public static void main2(String[] args) {
        System.out.println(max(12,11));
        System.out.println(max(12.2,32.1));
        System.out.println(max(12 ,22,11));
    }
    //斐波那契数列，迭代方式实现（循环）
    public static int fib1(int n)
    {
        if(n==1 || n==2)
            return 1;
        int f1 = 1;
        int f2 = 2;
        int f3 = -1;
        while(n-2 != 0){
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
            n--;
        }
        return f3;
    }

    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数：");
        int in= scanner.nextInt();
        System.out.println(fib1(in));
    }
}
